package com.hcyy.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hcyy.entity.Leibie;
import com.hcyy.entity.Product;
import com.hcyy.entity.User;
import com.hcyy.service.LeibieService;
import com.hcyy.service.ProductService;
import com.hcyy.util.UserUtil;

@Controller
public class ProductController {
	@Autowired
	private ProductService productService;
	@Autowired
	private LeibieService leibieService;
	@Autowired
	private UserUtil userUtil;
	
	//首页
	@RequestMapping("/index")
	public String showProduct(Model model,HttpServletRequest request){
		List<Product> productList=new ArrayList<Product>();
		List<Leibie> leibieList=new ArrayList<Leibie>();
		String dwbh=null;
		Integer tejia=null;//特价
		String a_tejia=request.getParameter("tejia");
		if(a_tejia!=null){
			tejia=Integer.parseInt(request.getParameter("tejia"));
		}
		Integer	sjid=1;
		Integer khlb=null;
		try {
			if(request.getParameter("sjid")!=null){
				sjid=Integer.parseInt(request.getParameter("sjid"));
//				ServletContext application = request.getServletContext();
//				application.setAttribute("sjid", sjid);//将sjid存入application中
			}
			else if(request.getSession().getAttribute("sjid")!=null){
				sjid=(Integer) request.getSession().getAttribute("sjid");
			}
			if(request.getSession().getAttribute("user")!=null){
				User user=userUtil.findUser(request);
				dwbh=user.getDwbh();
				khlb=user.getKhlb();
			}
			productList=productService.findProduct4Index(dwbh,tejia,sjid,khlb);
			leibieList=leibieService.findLeibie4Index();
			model.addAttribute("leibieList",leibieList);
			model.addAttribute("productList",productList);
			model.addAttribute("sjid",sjid);
		} 
		catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return "index";
	}
	
	//新品
	@RequestMapping("/new")
	public String findNewProduct(@RequestParam(defaultValue="1")Integer sjid,HttpServletRequest request,Model model){
		String dwbh = null;
		Integer khlb = null;
		if(request.getSession().getAttribute("user")!=null){
			User user=userUtil.findUser(request);
			dwbh=user.getDwbh();
			khlb=user.getKhlb();
		}
		List<Product> newProducts = new ArrayList<Product>();
		List<Product> productList = productService.findProductPage(0, 600, dwbh, null, sjid, khlb);
		for(Product product : productList){
			if(product.getZengsong()!=null){
				newProducts.add(product);
			}
		}
		model.addAttribute("productList",newProducts);
		model.addAttribute("sjid",sjid);
		model.addAttribute("url","new");
		return "index";
	}
	
	//首页 分类
	@RequestMapping("/ProductList")
	public String findProductByLeibie(Integer leibie,Integer sjid,Model model,HttpServletRequest request){
		List<Product> productList=new ArrayList<Product>();
		List<Leibie> leibieList=new ArrayList<Leibie>();
		String dwbh=null;
		Integer khlb=null;
		try {
			if(sjid==null){
				if(request.getSession().getAttribute("sjid")!=null){//已登录
					sjid=(Integer) request.getSession().getAttribute("sjid");
				}else{
					sjid=1;//未登录，默认设为1（乐赛仙）
				}
			}
			if(request.getSession().getAttribute("user")!=null){
				User user=userUtil.findUser(request);
				dwbh=user.getDwbh();
				khlb=user.getKhlb();
			}
			productList=productService.findProductByLeibie(dwbh,leibie,sjid,khlb,0,12);
			leibieList=leibieService.findLeibie4Index();
			model.addAttribute("productList",productList);
			model.addAttribute("leibieList",leibieList);
			model.addAttribute("sjid",sjid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "index";
	}
	
	//快速下单页
	@RequestMapping("Shop/QuickCart")
	public String quickCart(Model model,HttpServletRequest request){
		List<Product> productList=new ArrayList<Product>();
		String dwbh=null;
		Integer khlb=null;
		try {
			if(request.getSession().getAttribute("user")!=null){
				User user=userUtil.findUser(request);
				dwbh=user.getDwbh();
				khlb=user.getKhlb();
			}
			Integer sjid=(Integer) request.getSession().getAttribute("sjid");
			productList=productService.showProduct(dwbh,sjid,khlb);
			model.addAttribute("quickcartList",productList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "quickcart";
	}
	
	//商品详情页
	@RequestMapping("/Shop/Detail")
	public String productDetail(@RequestParam("productid")String productid,Model model,HttpServletRequest request){
//		String userid=userUtil.findUser(request).getDwbh();
		Integer sjid=(Integer) request.getSession().getAttribute("sjid");
		Product product=productService.findProductById(productid,sjid);
		model.addAttribute("product",product);
//		model.addAttribute("userid",userid);
		return "detail";
	}
	
	//首页、快速下单页，加载更多，分页  （有分类分页）
	@ResponseBody
	@RequestMapping(value="/AddMore",method=RequestMethod.POST)
	public List<Product> addMore(@RequestBody Map<String,Object> map,Model model,HttpServletRequest request){
		Integer offset=(Integer) map.get("offset");
		Integer size=(Integer) map.get("size");
		Integer leibie=(Integer) map.get("leibie");
		Integer tejia=(Integer) map.get("tejia");//特价
		String dwbh=null;
		Integer khlb=null;
		List<Product> productList=null;
		try {
			Integer sjid = (Integer) map.get("sjid");//未登录，首页加载更多，sjid从前台传入（index页面，登录和未登录，sjid不同）
			if(sjid==null){
				sjid=(Integer) request.getSession().getAttribute("sjid");//登录成功之后，快速下单页加载更多，读取session中的sjid；
			}
			if(request.getSession().getAttribute("user")!=null){
				User user=userUtil.findUser(request);
				dwbh=user.getDwbh();
				khlb=user.getKhlb();
			}
			if(leibie!=null){
				productList=productService.findProductByLeibie(dwbh, leibie, sjid, khlb, offset, size);
			}else{
				productList=productService.findProductPage(offset,size,dwbh,tejia,sjid,khlb);
			}
		} catch (NumberFormatException nfe) {
			nfe.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return productList;
	}
	
	//首页搜索
	@ResponseBody
	@RequestMapping(value="/Search", method=RequestMethod.POST)
	public List<Product> searchProduct4Index(HttpServletRequest request){
		 String search=request.getParameter("search");
		 String dwbh=null;
		 Integer khlb=null;
		 List<Product> productList=null;
		 try {
			 Integer sjid=Integer.parseInt(request.getParameter("sjid"));
			 if(request.getSession().getAttribute("user")!=null){
				 User user=userUtil.findUser(request);
				 dwbh=user.getDwbh();
				 khlb=user.getKhlb();
			 }
			 productList=productService.findProductBySearch(search.trim(),dwbh,sjid,khlb);
		 } catch (NumberFormatException e) {
			 e.printStackTrace();
		 } catch (Exception e) {
			 e.printStackTrace();
		 }
		 return productList;
	}
}
